package com.mxw.leetcode.B2链表双指针;

import com.mxw.leetcode.B1链表.entity.ListNode;

/**
 * @author Administrator
 * @create 2024/3/13 19:19
 */
public class A02反转链表 {

    /**
     题意：反转一个单链表。
     示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL
     */
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode cur = head;
        ListNode temp = null;
        while (cur != null) {
            temp = cur.next;// 保存下一个节点
            cur.next = prev;
            prev = cur;
            cur = temp;
        }
        return prev;
    }

}
